/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.feeds.JFeedType1;
import gui.feeds.JFeedType2;
import gui.feeds.JFeedType3;
import gui.feeds.JFeedType4;
import gui.feeds.JFeedType7;
import gui.feeds.JFeedType8;

import java.util.List;

import javax.swing.GroupLayout;

import core.exceptions.DBAccessException;
import core.objects.NewsFeedItem;
import db.DBAccessEngine;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

/**
 *
 * @author abadi
 */
public class JPanelNewsFeed extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	
	private int userId = 0;
	
	/**
     * Creates new form JPanelNewFeed
     */
    public JPanelNewsFeed(int userId) {
        initComponents();
        this.userId = userId;
        GUIUtils.setImageAtSize(jLabelSearchLoading, 80, 80, "/resources/waiting.gif");
        loadNewsFeed();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanelContents = new javax.swing.JPanel();
        jPanelLoading = new javax.swing.JPanel();
        jLabelSearchLoading = new javax.swing.JLabel();

        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        jScrollPane1.setOpaque(false);

        jPanelContents.setBackground(new java.awt.Color(51, 153, 255));
        jPanelContents.setAlignmentX(0.5F);
        jPanelContents.setLayout(new javax.swing.BoxLayout(jPanelContents, javax.swing.BoxLayout.PAGE_AXIS));

        jPanelLoading.setOpaque(false);
        jPanelLoading.add(jLabelSearchLoading);

        jPanelContents.add(jPanelLoading);

        jScrollPane1.setViewportView(jPanelContents);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelSearchLoading;
    private javax.swing.JPanel jPanelContents;
    private javax.swing.JPanel jPanelLoading;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
    private void loadNewsFeed() {
        jPanelContents.setVisible(false);
        jPanelContents.setVisible(true);
        jPanelLoading.setVisible(true);

        DBAccessThread.invoke(new IDBAccessThread() {
            @Override
            public Object onCreateQuery() throws DBAccessException {
				return DBAccessEngine.NewsFeed().load(userId);
            }

			@SuppressWarnings("unchecked")
			@Override
            public void onAccessFinished(Object response) {
                
                List<NewsFeedItem> feeds = (List<NewsFeedItem>) response;
                for (NewsFeedItem feed : feeds) {
                	switch (feed.getType()) {
	                	case LoanedFromMeLoaning:
	                	case LentFromAreLoaningTo:
	                		jPanelContents.add(new JFeedType1(feed));	                		
	                		break;
	                	case BookICommentedOnComment:
	                		jPanelContents.add(new JFeedType2(feed));
	                		break;
	                	case UserICommentedOnComment:
	                		jPanelContents.add(new JFeedType3(feed));
	                		break;
	                	case LentFromAreLoaningFrom:
	                	case LoanedFromMeLoaningTo:
	                		jPanelContents.add(new JFeedType4(feed));	                		
	                		break;
	                	case CommentsAboutMe:
	                		jPanelContents.add(new JFeedType7(feed));
	                		break;
	                	case NewUser:
	                		jPanelContents.add(new JFeedType8(feed));
	                		break;
                	}
                }
                
                jPanelLoading.setVisible(false);
                jPanelContents.setVisible(false);
                jPanelContents.setVisible(true);
                if (jPanelContents.getComponents().length == 0){
                    jPanelContents.add(GUIUtils.getNotFoundPanel());
                }
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                GUIUtils.showMessageError(e, jPanelContents);
                jPanelLoading.setVisible(false);
            }
        });
        
    }
}
